﻿using System;
using System.Collections.Generic;

namespace AssemblerCompiler
{
    public class TranslationResult
    {
        private readonly List<Line> cmdList;
        private readonly Dictionary<int, string> errorReports;
        private readonly List<SymTabElement> symTab;
        private Byte[] code;

        public TranslationResult()
        {
            this.cmdList = new List<Line>();
            this.symTab = new List<SymTabElement>();
            this.errorReports = new Dictionary<int, string>();
        }

        public int ProtocolLength
        {
            get
            {
                return this.cmdList.Count;
            }
        }

        public int SymTabLength
        {
            get
            {
                return this.symTab.Count;
            }
        }

        public Byte[] Code
        {
            get
            {
                return this.code;
            }
        }

        public Dictionary<int, string> ErrorReports
        {
            get
            {
                return this.errorReports;
            }
        }

        public List<SymTabElement> SymTab
        {
            get
            {
                return this.symTab;
            }
        }

        public void AddProtocolRow(Line nextRow)
        {
            this.cmdList.Add(nextRow);
        }

        public Line GetProtocolRow(int ind)
        {
            return this.cmdList[ind];
        }

        public void AddSymTabElement(SymTabElement nextSymTabElement)
        {
            this.symTab.Add(nextSymTabElement);
        }

        public bool SymTabContains(string name, SymTabElement.SymType type)
        {
            for (int i = 0; i < this.symTab.Count; ++i)
            {
                if (this.symTab[i].Name == name && this.symTab[i].Type == type)
                {
                    return true;
                }
            }
            return false;
        }

        public void SetLength(int length)
        {
            this.code = new Byte[length];
        }

        public UInt16 SymTabGetAddress(string name, SymTabElement.SymType type)
        {
            for (int i = 0; i < this.symTab.Count; ++i)
            {
                if (this.symTab[i].Name == name && this.symTab[i].Type == type)
                {
                    return this.symTab[i].Address;
                }
            }
            throw new Exception("Internal compiler error!");
        }
    }
}